// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    goodsCart:[],
    allChecked:false,
    totalNum:0,
    totalPrice:0
  },
  onShow:function(){
    //获取购物地址
    const address = wx.getStorageSync('address');
     //获取缓存的购物信息
     const goodsCart = wx.getStorageSync('cart')||[];
     let checkedCart = goodsCart.filter(v=>v.isActive);
      //获取总价格 总数量
      let totalNum = 0;
      let totalPrice=0;
      checkedCart.forEach(function(v,index){
         totalNum +=v.num;
         totalPrice+=v.goods_price*v.num;
      })
      this.setData({
       totalNum,
       totalPrice,
       address,
      goodsCart:checkedCart,
     })
  },
  handleChooseAddress:function(e){
    let that = this;
    wx.getSetting({
     success:function(res){
        // console.log(res);
        const scopeAddress = res.authSetting["scope.address"];
        if(scopeAddress===true || scopeAddress===undefined){
          wx.chooseAddress({
            success:function(res){
                wx.setStorageSync('address', res);
                that.setData({
                  address:res
                })
            }
          })
        }else{
          wx.openSetting({
            success:function(res2){
              wx.chooseAddress({
                success:function(res3){
                  wx.setStorageSync('address', res);
                  that.setData({
                    address:res
                  })
                }
              })
            }
          })
        }
        
     }
    })
      
  }
})